package com.tadpole.music.utils;


import android.os.Build;

import androidx.annotation.RequiresApi;

import com.google.gson.Gson;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 解析的工具类
 */
public class JsonParseUtil {
    /**
     * 解析实体 (最外层是jsonObject对象)
     */
    public static <T> T fastBean(String response, Class<T> classOfT) {
        Gson gson = new Gson();
        return gson.fromJson(response, classOfT);
    }

    /**
     * 解析实体
     * list里面还有实体类对象
     */
    public static <T> List<T> fastBeanList(String response, Class<T> classOfT) {
        Gson gson = new Gson();
        List<T> list = new ArrayList<>();
        JSONArray jsonArray = null;
        try {
            jsonArray = getData(response).getJSONArray("list");
            for (int i = 0; i < jsonArray.length(); i++) {
                list.add(gson.fromJson(jsonArray.get(i).toString(), classOfT));
            }
        } catch (JSONException e) {
            e.printStackTrace();
            return null;
        }
        return list;
    }

    public static <T> List<T> fastBeanListDataArr(String response, Class<T> classOfT) {
        Gson gson = new Gson();
        List<T> list = new ArrayList<>();
        JSONArray jsonArray = null;
        try {
            jsonArray = getDataArr(response);
            for (int i = 0; i < jsonArray.length(); i++) {
                list.add(gson.fromJson(jsonArray.get(i).toString(), classOfT));
            }
        } catch (JSONException e) {
            e.printStackTrace();
            return null;
        }
        return list;
    }
    /**
     * 将response字符串转JSONObject
     */
    public static JSONObject getJSONObject(String response) {
        JSONObject object = null;
        try {
            object = new JSONObject(response);
        } catch (JSONException e) {
        }
        return object;
    }


    /**
     * 将reponse里的data字符串转JSONObject
     */
    public static JSONObject getData(String response) {
        try {
            return getJSONObject(response).getJSONObject("data");
        } catch (JSONException e) {

        }
        return null;
    }

    /**
     * data字符串转JSONObject
     */
    public static JSONArray getDataArr(String response) {
        try {
            return getJSONObject(response).getJSONArray("dataArr");
        } catch (JSONException e) {

        }
        return null;
    }

    public static String ListToStr(List list){
        Gson gson2=new Gson();
        String str=gson2.toJson(list);
        if(str.equals("")||str==null){
            return "";
        }else{
            return str.substring(1,str.length()-1);
        }
    }

    /**
     * 通过键获取对应值
     *
     * @param object JSONObject对象
     * @param key    键
     * @return
     */
    public static String getString(JSONObject object, String key) {
        try {
            return object.getString(key);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 通过键获取对应值
     *
     * @param object JSONObject对象
     * @param key    键
     * @return
     */
    public static boolean getBoolean(JSONObject object, String key) {

        try {
            return object.getBoolean(key);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return false;
    }
    /**
     * 通过键获取对应值
     *
     * @param object JSONObject对象
     * @param key    键
     * @return
     */
    public static int getInt(JSONObject object, String key) {

        try {
            return object.getInt(key);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return -1;
    }
    /**
     * 通过键获取对应值
     *
     * @param object JSONObject对象
     * @param key    键
     * @return
     */
    public static long getLong(JSONObject object, String key) {

        try {
            return object.getLong(key);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return -1;
    }
    /**
     * 通过键获取对应值
     *
     * @param object JSONObject对象
     * @param key    键
     * @return
     */
    public static double getDouble(JSONObject object, String key) {

        try {
            BigDecimal bg = new BigDecimal(object.getDouble(key)).setScale(2, RoundingMode.HALF_UP);
            return  bg.floatValue();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return -1;
    }
    /**
     * 通过键获取对应值
     *
     * @param object JSONObject对象
     * @param key    键
     * @return
     */
    public static String getStr(JSONObject object, String key) {

        try {
            String str =  object.getString(key)==null?"":object.getString(key);
            return ("null".equals(str))?"":str;
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return "";
    }


    /**
     * 提示错误信息
     *
     * @param response totalpage
     * @return
     */
    public static String getMessage(String response) {
        JSONObject object = null;
        try {
            object = new JSONObject(response);
            return object.getString("msg");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * @param response
     * @return
     */
    public static String getStringStatus(String response) {
        JSONObject object = null;
        try {
            object = new JSONObject(response);
            return object.getString("status");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return "";
    }

    /***
     * 便利店解析店主推荐、个性标签，一级标签使用
     * @param obj
     * @return
     */
    public static <T> List<T> parseStoreJsonArray(String obj, Class<T> classOfT) {

        Gson gson = new Gson();
        List<T> list = new ArrayList<>();

        try {
            JSONArray arr=new JSONArray(obj);
            for (int i = 0; i < arr.length(); i++) {
                list.add(gson.fromJson(arr.get(i).toString(), classOfT));
            }
        } catch (JSONException e) {
            e.printStackTrace();
            return null;
        }
        return list;
    }


    /**
     *读取本地的json 文件，生成json字符串
     */
    public static String ReadFile(InputStream is){
        BufferedReader reader = null;
        StringBuffer sb = new StringBuffer();
        try{
            InputStreamReader inputStreamReader = new InputStreamReader(is, "UTF-8");
            reader = new BufferedReader(inputStreamReader);
            String tempString = null;
            while((tempString = reader.readLine()) != null){
                sb.append(tempString);
            }
            reader.close();
        }catch(IOException e){
            e.printStackTrace();
        }finally{
            if(reader != null){
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return sb.toString();
    }

}
